const util = require('../../utils/util');
const API = require('../../common/constant');
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    oldNumber: '***********',
    Number: '***********',
    code: '',
    sendStatus: false,
    seccond: 59
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getOldNumber();
  },
  /**
   * 获取旧的手机号码
   */
  getOldNumber: function () {
    util.http({
      url: API.URL.USER.FIND_MOBILE,
      success: res => {
        let oldnum = this.setoldNumber(res.data.bussData.split(''));
        this.setData({
          Number: res.data.bussData,
          oldNumber: oldnum
        });
      },
      fail: (rej) => {
        console.log(rej);
      }
    })
  },
  /**
   * 给手机号打码
   */
  setoldNumber: function (arr) {
    let newarr = arr.map((item, index) => {
      if (index > 2 && index < 7) {
        item = '*';
      }
      return item;
    });
    return newarr.join('');
  },
  /**
   * 获取手机验证码
   */
  sendPhoneCode: function () {
    util.http({
      url: API.URL.USER.FIND_MOBILECODE,
      data: {
        mobile: this.data.Number
      },
      success: res => {
        this.setData({
          code: res.data.bussData,
          sendStatus: true
        });
        app.showUserInfo('验证码已发送', 'success', 1500);
        let time = setInterval(() => {
          this.setData({
            seccond: this.data.seccond - 1
          });
          if (this.data.seccond == 0) {
            clearInterval(time);
            this.setData({
              sendStatus: false,
              seccond: 59
            })
          }
        }, 1000);
        console.log('发送成功');
      },
      fail: rej => {
        console.log(rej);
      }
    });
  },
  /**
   * 验证验证码
   */
  getInput: function (e) {
    console.log(e.detail);
    console.log('ajja');
    console.log(this.data.code);
    if (this.data.code.length == 0) {
      wx.showToast({
        title: '请点击获取验证码',
        icon: "none",
        duration: 1500
      });
    } else if (e.detail.value.checkcode == '') {
      wx.showToast({
        title: '请输入验证码',
        icon: "none",
        duration: 1500
      });
    } else if (e.detail.value.checkcode !== this.data.code) {
      wx.showToast({
        title: '验证码错误',
        icon: "none",
        duration: 1500
      });
    } else {
      wx.navigateTo({
        url: 'changePhoneNumber/changePhoneNumber'
      });
    }

  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})